package com.superteam.mac.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.superteam.mac.mode.RealTimeBean;

@SuppressWarnings("unchecked")
public class ReceiveData extends HttpServlet {

	private static final long serialVersionUID = -5006530323546381086L;
	public static List<Map<String,List<RealTimeBean>>> datas = new ArrayList();
	
	public static void addData(Map<String,List<RealTimeBean>> data)
	{
		datas.add(data);
		if(datas.size()>100)
		{
			datas.remove(0);
			
		}
		for(Map<String,List<RealTimeBean>> da:datas)
		{
			for(java.util.Iterator<String> iter = da.keySet().iterator();iter.hasNext();)
			{
				String dt = iter.next();
				List<RealTimeBean> dat = da.get(dt);
				for(RealTimeBean s:dat)
				{
					//System.out.println(s.getWorkShopName()+"   "+s.getMachineCode()+"  "+s.getTime()+" "+s.getMachineName());
				}
			}
		}
	}
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*ServletContext sc = request.getSession().getServletContext();
		ApplicationContext ctx =  WebApplicationContextUtils.getWebApplicationContext(sc);
		WorkShopDAO wrokShopDAO = (WorkShopDAO)ctx.getBean("wrokShopDAO");
		System.out.println(wrokShopDAO);*/
		
		//request.setCharacterEncoding("GBK");
		String data = request.getParameter("data");		
		System.out.println("====="+data);
    	String [] strs = data.split("\\|");
    	Map<String,List<RealTimeBean>> oncedata = new HashMap<String,List<RealTimeBean>>();
    	for(String str:strs){
    		String [] rs = str.split(",");//车间1,0101,主轴转速,2011-4-3 19:11:29,11|
    		RealTimeBean bean = new RealTimeBean();
	    		bean.setWorkShopName(rs[0]);
	    		bean.setMachineCode(rs[1]);
	    		bean.setIndexConfig(rs[2]);
	    		bean.setTime(rs[3]);
	    		bean.setConfigVal(rs[4]);
	    		
	    		bean.setMachineName(RealTimeUtil.getMachineName(rs[1]));
     		if(oncedata.containsKey(bean.getMachineCode()))
    		{
    			List<RealTimeBean> list = oncedata.get(bean.getMachineCode());
    			list.add(bean);
    			
    		}else
    		{
    			List<RealTimeBean> list = new ArrayList<RealTimeBean>();
    			list.add(bean);
    			oncedata.put(bean.getMachineCode(), list);
    		}
    		
    	}
    	addData(oncedata);
		java.io.PrintWriter out = response.getWriter();
		out.println("<staut>OK</staut>");
		out.flush();
		out.close();
	}

}
